

function say() {
    alert("437. 路径总和 III,后5%,待优化");
}
say();

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} sum
 * @return {number}
 */


var pathSum = function (root, sum) {
    //9:50-10:05   8:20-35
    //类似之前的,之前怎么做的
    //双层递归
    var res = []

    if(!root){
        return 0
    }

    dfs(root, [], 0, sum, res)
    var firstCount = res.length
    return firstCount + pathSum(root.left, sum) + pathSum(root.right, sum)
};

function dfs(root, arr, count, sum, res) {
    if (!root) {
        return
    } else {
        arr.push(root.val)
        count += root.val

        if (count == sum) {
            res.push(arr.slice())
        }
        dfs(root.left, arr, count, sum, res)
        dfs(root.right, arr, count, sum, res)
        count -= arr.pop()
    }
}
